package com.ecoolex.hotel.saas.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.hotel.saas.core.dao.DistributionDAO;
import com.ecoolex.hotel.saas.entity.Distribution;
import com.ecoolex.hotel.saas.entity.Distribution.Column;
import com.ecoolex.hotel.saas.entity.DistributionExample;
import com.ecoolex.hotel.saas.entity.DistributionExample.Criteria;
import com.ecoolex.hotel.saas.enums.DistributionType;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DistributionManager {
	
    @Autowired
    private DistributionDAO dao;
    
    public Distribution getDistribution(Integer distributionId) {
		return dao.selectByPrimaryKeyWithLogicalDelete(distributionId, Distribution.NOT_DELETED);
	}
    
    public Distribution getByDepartmentIdAndType(Integer departmentId, Integer type) {
    	DistributionExample example = new DistributionExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andDepartmentIdEqualTo(departmentId).andTypeEqualTo(type).andDeletedEqualTo(Distribution.NOT_DELETED);
		return dao.selectOneByExample(example);
	}

	public Distribution addDistribution(Distribution add, String createUser){
		Distribution old = getByDepartmentIdAndType(add.getDepartmentId(),add.getType());
		BizAssert.isTrue(Check.isNull(old),ResultCode.BIZ.build(10), "不能重复添加分销配置");
		if(Check.eq(add.getType(), DistributionType.PROPORTION)) {
			BizAssert.isTrue(Check.notNull(add.getDistributionRatio()),ResultCode.BIZ.build(11), "分销比例不能为空");
			BizAssert.isTrue(
				Check.isGreatThanEqual(add.getDistributionRatio(), BigDecimal.ZERO)
				&& Check.isLessThan(add.getDistributionRatio(), BigDecimal.valueOf(100)),
				ResultCode.BIZ.build(12), "分销比例应在0-100之间");
		}
		if(Check.eq(add.getType(), DistributionType.AMOUNT)) {
			BizAssert.isTrue(Check.notNull(add.getDistributionAmt()),ResultCode.BIZ.build(12), "分销金额不能为空");
		}
		add.setDeleted(Distribution.NOT_DELETED);
		add.setCreateUser(createUser);
		add.setCreateTime(Date.from(Instant.now()));
		add.setUpdateUser(createUser);
		add.setUpdateTime(Date.from(Instant.now()));
		dao.insert(add);
		return add;
	}

	public Distribution editDistribution(Distribution edit, String updateUser){
		edit.setUpdateUser(updateUser);
		edit.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(edit, Column.distributionAmt,Column.distributionRatio,Column.updateUser,Column.updateTime);
		return edit;
	}
    
    public List<Distribution> queryByDepartmrntId(Integer departmentId) {
    	DistributionExample example = new DistributionExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andDepartmentIdEqualTo(departmentId).andDeletedEqualTo(Distribution.NOT_DELETED);
    	return dao.selectByExample(example);
    }
    
    public void deleteByDepartmentId(Integer departmentId, String updateUser) {
    	queryByDepartmrntId(departmentId).forEach(d -> {
    		d.setDeleted(Distribution.IS_DELETED);
    		d.setUpdateUser(updateUser);
    		d.setUpdateTime(Date.from(Instant.now()));
    		dao.updateByPrimaryKeySelective(d, Column.deleted,Column.updateUser,Column.updateTime);
    	});
    }
}